package com.neteast.androidclient.newscenter.domain;

import java.util.ArrayList;
import java.util.Collections;
import android.util.Log;

public class InfoCache{
	
	private ArrayList<Information> store=new ArrayList<Information>();
	
	public InfoCache() {}
	
	public InfoCache(int winHeight,ArrayList<Information> infos) {
		init(infos);
	}
	
	private void init(ArrayList<Information> infos) {
		if (infos!=null) {
			store.addAll(infos);
			Collections.sort(store);
		}
	}
	
	/**
	 * 往消息缓存里插入一条新消息
	 * @param info
	 */
	public boolean addInfo(Information info) {
		if (!store.contains(info)) {
			store.add(info);
			Collections.sort(store);
			Log.i("test", "put a new info in cache");
			return true;
		}else {
			Log.i("test", "info has exists");
			return false;
		}
	}
	
	public void addInfos(ArrayList<Information> infos) {
		init(infos);
	}
	/**
	 * 删除一条消息
	 * @param info
	 */
	public void deleteInfo(Information info) {
		store.remove(info);
		Collections.sort(store);
	}
	/**
	 * 清空缓存
	 */
	public void clearCache() {
		store.clear();
	}
	
	public ArrayList<Information> getData() {
		return store;
	}
	
	public Information get(int index) {
		return store.get(index);
	}
}
